<template>
	<view style="padding: 20rpx;">
		<uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="button"
			activeColor="#127add"></uni-segmented-control>
		<view class="content">
			<view v-show="current === 0">
				<view class="box" v-for="item in patientOrderList">
					<view class="item">
						<view class="text-left">{{item.orderNo}}</view>
						<view class="text-right">
							<text style="color:#b23209"   v-if="item.status==='-1'">已取消</text>
							<text style="color:#b79432" v-else-if="item.status==='0'">待支付</text>
							<text style="color:#1a9332" v-else-if="item.status==='1'">已支付</text>
							<text v-else-if="item.status==='2'">已叫号</text>
						</view>
					</view>
					 <view class="divider"></view>
					<view class="item">
						<view class="text-left">就诊科室</view>
						<view class="text-right">{{item.appointmentScheduler.doctor.department.name}}</view>
					</view>
					<view class="item">
						<view class="text-left">就诊时间</view>
						<view class="text-right">{{item.appointmentScheduler.workDate}}&ensp;{{item.appointmentTime}}</view>
					</view>
					<view class="item">
						<view class="text-left">挂号费用</view>
						<view class="text-right">￥{{item.amount}}</view>
					</view>
					<view class="operation-item">
						<view>
						<button v-if="item.status==='0'" size="mini" style="margin-right: 20rpx;" type="primary">支付</button>
						<button v-if="item.status==='0' || item.status==='1'"  size="mini" style="margin-right: 20rpx;" type="warn"  @click="cancelOrder(item.orderNo)">取消预约</button>
						<button v-if="item.status==='2' && item.certificateNo !=null" size="mini" style="margin-right: 20rpx;background-color: #588ef2;color: white;" @click="downloadMedicalCertificate(item.certificateNo)">下载诊断书</button>
						<button  size="mini">查看详情</button>
						</view>
					</view>
				</view>
			</view>
			<view v-show="current === 1">
				<view class="box" v-for="item in patientOrderList">
					<view class="item">
						<view class="text-left">{{item.orderNo}}</view>
						<view class="text-right">
							<text style="color:#b79432">待支付</text>
						</view>
					</view>
					 <view class="divider"></view>
					<view class="item">
						<view class="text-left">就诊科室</view>
						<view class="text-right">{{item.appointmentScheduler.doctor.department.name}}</view>
					</view>
					<view class="item">
						<view class="text-left">就诊时间</view>
						<view class="text-right">{{item.appointmentScheduler.workDate}}&ensp;{{item.appointmentTime}}</view>
					</view>
					<view class="item">
						<view class="text-left">挂号费用</view>
						<view class="text-right">￥{{item.amount}}</view>
					</view>
					<view class="operation-item">
						<view>
						<button  size="mini" style="margin-right: 20rpx;" type="primary">支付</button>
						<button  size="mini" style="margin-right: 20rpx;" type="warn"  @click="cancelOrder(item.orderNo)">取消预约</button>
						<button  size="mini">查看详情</button>
						</view>
					</view>
				</view>
			</view>
			<view v-show="current === 2">
				<view class="box" v-for="item in patientOrderList">
					<view class="item">
						<view class="text-left">{{item.orderNo}}</view>
						<view class="text-right">
							<text style="color:#1a9332">已支付</text>
						</view>
					</view>
					 <view class="divider"></view>
					<view class="item">
						<view class="text-left">就诊科室</view>
						<view class="text-right">{{item.appointmentScheduler.doctor.department.name}}</view>
					</view>
					<view class="item">
						<view class="text-left">就诊时间</view>
						<view class="text-right">{{item.appointmentScheduler.workDate}}&ensp;{{item.appointmentTime}}</view>
					</view>
					<view class="item">
						<view class="text-left">挂号费用</view>
						<view class="text-right">￥{{item.amount}}</view>
					</view>
					<view class="operation-item">
						<view>
						<button  size="mini" style="margin-right: 20rpx;" type="warn" @click="cancelOrder(item.orderNo)">取消预约</button>
						<button  size="mini">查看详情</button>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				items: ['全部', '待支付', '已支付'],
				current: 0,
				currentPage: 1,
				pageSize: 10000,
				userId: uni.getStorageSync('xm-user') ? uni.getStorageSync('xm-user').userId : null,
				status: '',
				patientOrderList: [],
			}
		},
		onLoad() {
			console.log(this.userId)
			this.load();
		},
		methods: {
			onClickItem(e) {
				if (this.current != e.currentIndex) {
					this.current = e.currentIndex;
				}
				if(this.current===0){
					this.status='';
				}else if(this.current===1){
					this.status='0';
				}else if(this.current===2){
					this.status='1';
				}
				this.load();
			},
			load() {
				let params = {
					currentPage: this.currentPage,
					pageSize: this.pageSize,
					userId: this.userId,
					status: this.status
				}
				this.$request.get('/order/findPatientOrderList',params).then(res => {
                   this.patientOrderList=res.data.records;
				   console.log(this.patientOrderList)
				})
			},
			cancelOrder(orderNo){
				this.$request.get('/order/cancel/'+orderNo).then(res=>{
					   if(res.code===200){
						   uni.showToast({
						   	title: '预约已取消',
						   	icon: 'success',
						   	position: 'top',
						   });
						   this.status='',
						   this.load();
						   this.current=0;
					   }else{
						   uni.showToast({
						   	title: JSON.stringify(res.msg),
						   	icon: 'error',
						   	position: 'top',
						   });
					   }
				})
			},
			downloadMedicalCertificate(certificateNo){
				uni.downloadFile({
					url: 'http://localhost:8089/medical-certificate/downloadMedicalCertificate/'+certificateNo, //仅为示例，并非真实的资源
					success: (res) => {
											if (res.statusCode === 200) {
												console.log('下载成功', res);
												let filePath = res.tempFilePath;
												uni.openDocument({
													filePath: filePath,
													showMenu: true,
													success: function(res) {
														console.log('打开文档成功');
													},
													fail:function(err) {
														console.log('err',err);
													}
												});
											}
										}
				});
			}

		}
	}
</script>

<style>
	.box {
		background-color: #fff;
		border-radius: 10rpx;
		line-height: 60rpx;
		font-weight: 500;
		margin-top: 20rpx;
		padding: 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, .1);
	}
    .item {
    	display: flex;
    	justify-content: space-between;
    }
	.operation-item{
		padding-top: 20rpx;
		display: flex;
		justify-content:flex-end;
	}
	.text-left {
		color: #8b8b8b;
		font-size: 28rpx;
	}

	.text-left {
		font-size: 28rpx;
	}
	.divider{
		height: 1rpx;
		background-color: #b1b1b1;
		margin: 10rpx 0;
	}
</style>